package cn.lf.android.lawfirm.downloadcontroller;

import android.content.Context;

import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.lf.android.lawfirm.AppConfig;
import cn.lf.android.lawfirm.BaseController;
import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.manager.MyOkHttpClient;
import cn.lf.android.lawfirm.downloadcontroller.modle.Agreement;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.JsonBeans;
import cn.lf.android.lawfirm.utils.SPUtil;

/**
 * 合同
 */

public class AgreementController extends BaseController {
    AgreementCallback mAgreementCallback;
    private Context context;

    public AgreementController(AgreementCallback callback, Context context) {
        this.mAgreementCallback = callback;
        this.context = context;
    }

    /**
     * @param relationSid 关联编号，读取概要接口返回的relationSid字段
     * @param typeID      附件类型：承办审请-1，新增审级-2，合同变更-3，请参照左侧对照说明，传送数字
     */
    public void onAgreement(String relationSid, String typeID) {
//        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("relationSid", relationSid);
        params.put("typeID", typeID);
        new MyOkHttpClient().doGet(AppConfig.LSCaseApproveRecordList_undertake_attachments, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("合同====" + response);
                        JSONObject myJsonObject;
                        String arraylist;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                arraylist = myJsonObject.getJSONArray("responsObject")
                                        .toString();
                                List<Agreement> beans = JsonBeans
                                        .getJsonList(
                                                arraylist,
                                                new TypeToken<List<Agreement>>() {
                                                });
                                mAgreementCallback.onAgreementSuccessed(beans);
                            } else {
                                mAgreementCallback
                                        .onAgreementFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
//                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        DebugLog.e("合同55====" + errorMsg);
                        mAgreementCallback.onAgreementFailed();
//                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }

    public interface AgreementCallback {
        public void onAgreementSuccessed(List<Agreement> beans);

        public void onAgreementFailed();

    }
}
